#include "modbustransfer.h"
#include "modbustransfer_p.h"

#include <unistd.h>

#include "phylink.h"
#include "dx_type.h"
#include "iot_debug.h"



ModbusTransfer::ModbusTransfer(IPhyLink *dev, ModbusTransfer::Mode mode) :
    private_(new ModbusTransferPrivate(dev, mode))
{

}

ModbusTransfer::~ModbusTransfer()
{
    delete private_;
}

bool ModbusTransfer::isTcp() const
{
    return private_->mode_ == TCP;
}

void ModbusTransfer::setPhyLink(IPhyLink *dev)
{
    private_->dev_ = dev;
}

void ModbusTransfer::setMode(ModbusTransfer::Mode mode)
{
    private_->mode_ = mode;
}

int ModbusTransfer::sendCmdAndRecv(const ModbusCmdInfo &cmd, uint8_t *recv_buf)
{
    return private_->sendCmdAndRecv(cmd, recv_buf);
}

int ModbusTransfer::setYKCmd(const ModbusYKCmdInfo &cmd)
{
    return private_->setYKCmd(cmd);
}

uint8_t ModbusTransfer::encode(const ModbusCmdInfo &cmd, uint8_t *buf, uint8_t buf_size)
{
    return private_->encode(cmd, buf, buf_size);
}

uint8_t ModbusTransfer::encode(const ModbusYKCmdInfo &cmd, uint8_t *buf, uint8_t buf_size)
{
    return private_->encode(cmd, buf, buf_size);
}

int ModbusTransfer::parseRTU(const uint8_t *rx_data, uint16_t rx_len, uint8_t slave_id, uint8_t func)
{
    return private_->parseRTU(rx_data, rx_len, slave_id, func);
}

bool ModbusTransfer::verifyCRC(const uint8_t *rx_data, uint16_t rx_len)
{
    return private_->verifyCRC(rx_data, rx_len);
}
